package com.fsnbigdata.domain;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fsnbigdata.util.ChineseInital;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;

import org.hibernate.validator.constraints.Length;
import org.springframework.roo.addon.javabean.annotations.RooEquals;
import org.springframework.roo.addon.javabean.annotations.RooJavaBean;
import org.springframework.roo.addon.javabean.annotations.RooToString;
import org.springframework.roo.addon.jpa.annotations.entity.RooJpaEntity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Version;
import javax.persistence.Basic;
import javax.persistence.FetchType;
import javax.persistence.Lob;
import javax.validation.constraints.NotNull;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.ManyToMany;
import io.springlets.format.EntityFormat;
import java.util.Objects;
import javax.persistence.Entity;
import javax.validation.constraints.Pattern;

/**
 * = Document
 *
 * TODO Auto-generated class documentation
 *
 */
@RooJavaBean
@RooToString
@RooJpaEntity(entityFormatExpression = "#{name}")
@RooEquals(isJpaEntity = true)
@Entity
@EntityFormat("#{name}")
public class Document {

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    @Version
    private Integer version;

    /**
     * 文献信息
     */
    @Lob
    @Basic(fetch = FetchType.LAZY)
    @NotNull
    @Length(min = 0, max = 1000,message ="文献信息内容长度必须在0-1000个字符之间")
    private String name;

    /**
     * 拼音首字母
     */
    private String initial;

    /**
     * 作者
     */
    @Lob
    @Basic(fetch = FetchType.LAZY)
    @NotNull
    @Length(min = 0, max = 1000,message ="作者信息内容长度必须在0-1000个字符之间")
    private String author;

    /**
     * 时间
     */
    @Pattern(regexp = "^-?\\d+(\\.\\d+)?$",message = "时间只能输入数字类型")
    private String docYear;

    /**
     * 资源链接
     */
    private String url;

    /**
     * 创建者
     */
    private String creator;

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    @ManyToMany(fetch = FetchType.EAGER)
    @JsonIgnore
    private Set<Organism> organisms = new HashSet<Organism>();

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    @ManyToMany(fetch = FetchType.EAGER)
    @JsonIgnore
    private Set<Specimen> specimens = new HashSet<Specimen>();

    @ManyToMany(fetch = FetchType.EAGER)
    @JsonIgnore
    private Set<ZhDiversityReport> diversitys = new HashSet<ZhDiversityReport>();

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String ITERABLE_TO_ADD_CANT_BE_NULL_MESSAGE = "The given Iterable of items to add can't be null!";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String ITERABLE_TO_REMOVE_CANT_BE_NULL_MESSAGE = "The given Iterable of items to add can't be null!";

    /**
     * This `equals` implementation is specific for JPA entities and uses
     * the entity identifier for it, following the article in
     * https://vladmihalcea.com/2016/06/06/how-to-implement-equals-and-hashcode-using-the-jpa-entity-identifier/
     *
     * @param obj
     * @return Boolean
     */
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        // instanceof is false if the instance is null
        if (!(obj instanceof Document)) {
            return false;
        }
        return getId() != null && Objects.equals(getId(), ((Document) obj).getId());
    }

    /**
     * This `hashCode` implementation is specific for JPA entities and uses a fixed `int` value to be able
     * to identify the entity in collections after a new id is assigned to the entity, following the article in
     * https://vladmihalcea.com/2016/06/06/how-to-implement-equals-and-hashcode-using-the-jpa-entity-identifier/
     *
     * @return Integer
     */
    public int hashCode() {
        return 31;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return Long
     */
    public Long getId() {
        return this.id;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param id
     */
    public void setId(Long id) {
        this.id = id;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return Integer
     */
    public Integer getVersion() {
        return this.version;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param version
     */
    public void setVersion(Integer version) {
        this.version = version;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return String
     */
    public String getName() {
        return this.name;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return String
     */
    public String getInitial() {
        return this.initial;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param initial
     */
    public void setInitial(String initial) {
        this.initial = initial;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return String
     */
    public String getAuthor() {
        return this.author;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param author
     */
    public void setAuthor(String author) {
        this.author = author;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return String
     */
    public String getDocYear() {
        return this.docYear;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param docYear
     */
    public void setDocYear(String docYear) {
        this.docYear = docYear;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return String
     */
    public String getUrl() {
        return this.url;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param url
     */
    public void setUrl(String url) {
        this.url = url;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return String
     */
    public String getCreator() {
        return this.creator;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param creator
     */
    public void setCreator(String creator) {
        this.creator = creator;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return Set
     */
    public Set<Organism> getOrganisms() {
        return this.organisms;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param organisms
     */
    public void setOrganisms(Set<Organism> organisms) {
        this.organisms = organisms;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return Set
     */
    public Set<Specimen> getSpecimens() {
        return this.specimens;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param specimens
     */
    public void setSpecimens(Set<Specimen> specimens) {
        this.specimens = specimens;
    }

    public Set<ZhDiversityReport> getDiversitys() {
        return diversitys;
    }

    public void setDiversitys(Set<ZhDiversityReport> diversitys) {
        this.diversitys = diversitys;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return String
     */
    public String toString() {
        return "Document {" + "id='" + id + '\'' + ", version='" + version + '\'' + ", name='" + name + '\'' + ", initial='" + initial + '\'' + ", author='" + author + '\'' + ", docYear='" + docYear + '\'' + ", url='" + url + '\'' + ", creator='" + creator + '\'' + ", ITERABLE_TO_ADD_CANT_BE_NULL_MESSAGE='" + ITERABLE_TO_ADD_CANT_BE_NULL_MESSAGE + '\'' + ", ITERABLE_TO_REMOVE_CANT_BE_NULL_MESSAGE='" + ITERABLE_TO_REMOVE_CANT_BE_NULL_MESSAGE + '\'' + "}" + super.toString();
    }



    @PrePersist
    public void prePersist(){
        this.setInitial(ChineseInital.getFirstLetter(this.getName()));
    }

    @PreUpdate
    public void preUpdate(){
        this.setInitial(ChineseInital.getFirstLetter(this.getName()));
    }
}
